package impl;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import baise.BaseDaoImpl;
import baise.iHibBaseDAO;
import baise.iHibBaseDAOimpl;
import model.TBulletin;
import DAO.TBulletinDAO;
/**
 * 公告列表实现类
 * @author Administrator
 *
 */
public class TBulletinDAOimpl implements TBulletinDAO {
	iHibBaseDAO iBaseDAO=new iHibBaseDAOimpl();
	@Override
	public int addTBulletin(TBulletin tbull) {
		
		return (Integer) iBaseDAO.insert(tbull);
	}

	@Override
	public boolean upTBulletin(TBulletin tbull) {
		// TODO Auto-generated method stub
		return iBaseDAO.update(tbull);
	}

	@Override
	public boolean delectTBulletin(TBulletin tbull) {
		// TODO Auto-generated method stub
		return iBaseDAO.delete(tbull);
	}

	@Override
	public TBulletin seletcTBulletin(TBulletin tbull) {
		// TODO Auto-generated method stub
		return (TBulletin) iBaseDAO.findById(TBulletin.class,tbull.getId());
	}

	@Override
	public List<TBulletin> listTBull(String contentget) {
		String sql="select * from T_LibraryBulletin where contentget="+contentget;
		BaseDaoImpl bea=new BaseDaoImpl();
		ResultSet rs=bea.select(sql);
		List<TBulletin> list=null;
		try {
		 list=convertList(rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}

	@Override
	public List<TBulletin> listTBullall() {
		String sql="select * from T_LibraryBulletin";
		BaseDaoImpl bea=new BaseDaoImpl();
		ResultSet rs=bea.select(sql);
		List<TBulletin> list=null;
		try {
		 list=convertList(rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}
	private static List convertList(ResultSet rs) throws SQLException{
		List list = new ArrayList();
		ResultSetMetaData md = rs.getMetaData();//��ȡ����
		int columnCount = md.getColumnCount();//��ȡ�е�����
		while (rs.next()) {
			Map rowData = new HashMap();//����Map
				for (int i = 1; i <= columnCount; i++) {
					rowData.put(md.getColumnName(i), rs.getObject(i));//��ȡ����ֵ
				}
				list.add(rowData);
			}
		return list;
	}
//	public static void main(String[] args) {
//		TBulletinDAO tb=new TBulletinDAOimpl();
//		//tb.listTBullall();
//		
//		TBulletin tbull=new TBulletin();
//		tbull.setId(1);
//		String json=JSON.toJSONString(tb.seletcTBulletin(tbull));
//		System.out.println(json);
//		
//		System.out.println();
//	}

}
